package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired(required = false)
    private DishService dishService;

    @GetMapping("/page")
    public R<Page> page(Integer page ,Integer pageSize,String name){
        Page<DishDto> dishPage=dishService.page(page,pageSize,name);

        return R.success(dishPage);
    }
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto, HttpSession session){
        Long empId = (Long) session.getAttribute("employee");
        dishDto.setCreateUser(empId);
        dishDto.setUpdateUser(empId);
        dishService.saveWithFlavors(dishDto);
        return R.success("保存数据成功");
    }

    @GetMapping("/{id}")
    public R<DishDto> selectById(@PathVariable Long id ){
        DishDto pagedishDto=dishService.selectById(id);
        return R.success(pagedishDto);
    }


    //修改菜品
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto,HttpSession session){
        Long empId = (Long) session.getAttribute("employee");
        dishDto.setUpdateUser(empId);
        dishService.update(dishDto);
        return R.success("修改数据成功");
    }
    @GetMapping("/list")
    public R<List<DishDto>> findById(Long categoryId,Integer status){
        List<DishDto> dishList=dishService.findByIdlist(categoryId,status);
        return R.success(dishList);
    }
    @DeleteMapping
    public R<String> deletebatch(@RequestParam List<Long> ids){
        dishService.deletebatch(ids);
        return R.success("批量删除成功！");
    }
    @PostMapping("/status/0")
    public R<String> updataStatusZero(@RequestParam List<Long> ids){
        dishService.updateStatusZero(ids);
        return R.success("批量停售成功！");
    }
    //批量启售
    @PostMapping("/status/1")
    public R<String> updataStatusOne(@RequestParam List<Long> ids){
        dishService.updateStatusOne(ids);
        return R.success("批量启售成功！");
    }

}

